package com.rm.questions.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rm.questions.common.Result;
import com.rm.questions.entity.TrainRelaPaperToQuestion;
import com.rm.questions.service.ITrainRelaPaperToQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/trainRelaPaperToQuestion")
public class TrainRelaPaperToQuestionController {

    @Autowired
    private ITrainRelaPaperToQuestionService relaService;

    // 新增关联关系
    @PostMapping
    public Result add(@RequestBody TrainRelaPaperToQuestion rela) {
        boolean saved = relaService.save(rela);
        return saved ? Result.success("关联关系添加成功") : Result.error("关联关系添加失败");
    }

    // 删除关联关系
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Long id) {
        boolean removed = relaService.removeById(id);
        return removed ? Result.success("关联关系删除成功") : Result.error("关联关系删除失败");
    }

    // 根据试卷ID查询关联的题目
    @GetMapping("/byPaper/{paperId}")
    public Result getByPaperId(@PathVariable Long paperId) {
        QueryWrapper<TrainRelaPaperToQuestion> query = new QueryWrapper<>();
        query.eq("paper_id", paperId);
        return Result.success(relaService.list(query));
    }

    // 根据题目ID查询关联的试卷
    @GetMapping("/byQuestion/{questionId}")
    public Result getByQuestionId(@PathVariable Long questionId) {
        QueryWrapper<TrainRelaPaperToQuestion> query = new QueryWrapper<>();
        query.eq("question_id", questionId);
        return Result.success(relaService.list(query));
    }

    // 分页查询
    @GetMapping("/page")
    public Result page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        Page<TrainRelaPaperToQuestion> page = new Page<>(pageNum, pageSize);
        relaService.page(page);
        return Result.success(page);
    }
}